package com.code.soulspringboot;

import com.code.soulspringboot.code.BizResponseCode;
import com.code.soulspringboot.exception.BaseException;
import com.code.soulspringboot.exception.BizExceptionAssert;
import com.code.soulspringboot.exception.PointException;
import com.code.soulspringboot.handler.BaseResponse;
import com.code.soulspringboot.handler.ResponseProcessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lyf
 * @date 2022-11-20 21:18
 */
@RestController
@RequestMapping(("/exception"))
public class HelloController {

    @GetMapping("/hello")
    public BaseResponse<Boolean> hello(@RequestParam("msg") String msg) {

        // 这里是把函数和入参一起传递给 handle 方法处理
        return ResponseProcessor.handle(msg, s -> {
            // 异常断言处理
            BizExceptionAssert.isTrue(!"hello".equals(s), BizResponseCode.REPEAT_CODE, s);
            // 业务逻辑处理
            if ("world".equals(s)) {
                return true;
            }
            return false;
        });
    }

    @GetMapping("/hello2")
    public BaseResponse<Boolean> hello2() {

        return t();
    }

    private BaseResponse<Boolean> t() {
        return t2();
    }

    private BaseResponse<Boolean> t2() {
        try {
            int a  = 1;
            if (a == 1) {
                throw new PointException(BizResponseCode.REPEAT_CODE, "1001");
            }
        } catch (PointException e) {
            throw new BaseException(BizResponseCode.REPEAT_CODE, e, "1002");
        }
        return null;
    }
}
